﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using 海漫思考核.DAL;

namespace 海漫思考核.BLL
{
    public class NoveBLL
    {
        /// <summary>
        /// 按照时间排序和小说类型及小说名称查询前八条小说
        /// </summary>
        /// <param name="sort">排序方式 asc or desc</param>
        /// <param name="type">小说类型</param>
        /// <param name="name">小说名称</param>
        /// <param name="pageindex">页数</param>
        /// <returns></returns>
        public static List<view_NoveTypeState> SelectNove(string sort,string type,string name,int pageindex)
        {
            using(NoveEntities ef = new NoveEntities())
            {
                int newindex = pageindex * 9;
                if (sort == "asc")
                {
                    return ef.view_NoveTypeState.Where(a => a.Tname.Contains(type) && a.Nname.Contains(name)).OrderBy(a => a.Nuptime).Skip(newindex).Take(9).ToList();
                }
                else
                {
                    return ef.view_NoveTypeState.Where(a => a.Tname.Contains(type) && a.Nname.Contains(name)).OrderByDescending(a => a.Nuptime).Skip(newindex).Take(9).ToList();
                }
            }
        }
        /// <summary>
        /// 查询所有小说类型
        /// </summary>
        /// <returns></returns>
        public static List<NoveType> SelectAllNoveType()
        {
            using(NoveEntities ef = new NoveEntities())
            {
                return ef.NoveType.ToList();
            }
        }
        /// <summary>
        /// 按照ID删除小说
        /// </summary>
        /// <param name="nove"></param>
        /// <returns></returns>
        public static bool NoveDel(int id)
        {
            using(NoveEntities ef = new NoveEntities())
            {
                var nove = ef.NoveInfo.Where(a => a.Nid == id).FirstOrDefault();
                ef.NoveInfo.Remove(nove);
                return ef.SaveChanges() > 0;
            }
        }
        /// <summary>
        /// 按照ID查找小说
        /// </summary>
        /// <param name="id">小说ID</param>
        /// <returns></returns>
        public static NoveInfo SelectNoveByID(int id)
        {
            using(NoveEntities ef = new NoveEntities())
            {
                return ef.NoveInfo.Where(a => a.Nid == id).FirstOrDefault();
            }
        }
        /// <summary>
        /// 编辑小说
        /// </summary>
        /// <param name="newnove">小说新信息</param>
        /// <returns></returns>
        public static bool NoveUpdate(NoveInfo newnove)
        {
            using(NoveEntities ef = new NoveEntities())
            {
                var nove = ef.NoveInfo.Where(a => a.Nid == newnove.Nid).FirstOrDefault();
                nove.Nname = newnove.Nname;
                nove.Nauthor = newnove.Nauthor;
                nove.Nchapter = newnove.Nchapter;
                nove.Nuptime = newnove.Nuptime;
                nove.Ntype = newnove.Ntype;
                nove.Nstate = newnove.Nstate;
                return ef.SaveChanges() > 0;
            }
        }
        /// <summary>
        /// 小说总数
        /// </summary>
        /// <returns></returns>
        public static int NoveCount()
        {
            using(NoveEntities ef = new NoveEntities())
            {
                return ef.NoveInfo.Select(a => a.Nid).Count();
            }
        }
        /// <summary>
        /// 新增小说方法
        /// </summary>
        /// <returns></returns>
        public static bool NoveAdd(NoveInfo nove)
        {
            using(NoveEntities ef = new NoveEntities())
            {
                ef.NoveInfo.Add(nove);
                return ef.SaveChanges() > 0;
            }
        }
        /// <summary>
        /// 按照小说查询小说
        /// </summary>
        /// <param name="name">小说名称</param>
        /// <returns></returns>
        public static NoveInfo SelectNoveByName(string name)
        {
            using(NoveEntities ef = new NoveEntities())
            {
                return ef.NoveInfo.Where(a => a.Nname == name).FirstOrDefault();
            }
        }
    }
}